package com.zpark.transaction.service.controller;

import com.zpark.jushanpay.common.domain.BusinessException;
import com.zpark.jushanpay.common.domain.RestResponse;
import com.zpark.transaction.api.dto.PayChannelDTO;
import com.zpark.transaction.api.dto.PayChannelParamDTO;
import com.zpark.transaction.api.dto.PlatformChannelDTO;
import com.zpark.transaction.api.service.IPayChannelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @PackageName: com.zpark.transaction.service.controller
 * @ClassName: TranPayChannelController
 * @Description:
 * @author: RZS
 * @date: 2024/5/21  11:38
 */
@RestController
@RequestMapping("/pay_channel")
public class TranPayChannelController {

    @Autowired
    private IPayChannelService payChannelService;

    /**
     * 查询所有平台服务类型数据
     * @return
     */
    @GetMapping("/find_platform_channel_all")
    public RestResponse<List<PlatformChannelDTO>> findPlatformChannelAll(){
        // 1、调用业务层查询数据
        List<PlatformChannelDTO> dtos = payChannelService.queryPlatformChannelAll();
        // 2、返回结果
        return RestResponse.success(dtos);
    }

    /**
     * 应用绑定平台服务类型
     * @param appId 应用的appId
     * @param platformChannelCode 平台服务类型编码
     * @return
     */
    @PostMapping("/bind_platform_channel_app/{appId}")
    public RestResponse<Void> bindPlatformChannelForApp(
            @PathVariable("appId") String appId,
            @RequestParam("platformChannelCode") String platformChannelCode){
        // 调用业务层方法实现应用绑定
        payChannelService.bindPlatformChannelForApp(appId, platformChannelCode);

        return RestResponse.success();
    }

    /**
     * 查询应用与平台支付渠道类型是否绑定
     * @param appId 应用ID
     * @param platformChannelCode 平台支付渠道类型编码
     * @return
     */
    @PostMapping("/is_bind_app_platform_channel/{appId}")
    public RestResponse<Integer> findAppPlatformChannelByAppIdChannelCode(
            @PathVariable("appId") String appId,
            @RequestParam("platformChannelCode") String platformChannelCode){
        // 调用业务层接口查询数据
        Integer count = payChannelService.queryAppBindPlatformChannel(appId, platformChannelCode);

        return RestResponse.success(count);
    }

    /**
     * 通过平台服务类型编码查询原始支付渠道数据
     * @param platformChannelCode 平台服务类型编码
     * @return
     */
    @PostMapping("/find_pay_channel_by_platform_channel")
    public RestResponse<List<PayChannelDTO>> findPayChannelByPlatformChannel(
            @RequestParam("platformChannelCode") String platformChannelCode){

        // 调用业务层方法查询数据
        List<PayChannelDTO> payChannelDTOS =
                payChannelService.queryPayChannelByPlatformChannel(platformChannelCode);
        // 返回结果
        return RestResponse.success(payChannelDTOS);
    }

    /**
     * 保存支付渠道参数配置
     * @param dto 支付渠道参数对象
     * @return
     */
    @PostMapping("/save_pay_channel_param")
    public RestResponse<Void> addPayChannelParam(@RequestBody PayChannelParamDTO dto){
        // 调用业务层方法实现保存数据
        payChannelService.savePayChannelParam(dto);
        return RestResponse.success();
    }

    /**
     * 根据应用的appId和平台服务类型编码查询原始支付渠道参数配置数据
     * @param appId 应用的appId
     * @param platformChannelCode 平台服务类型编码
     * @return
     */
    @PostMapping("/find_pay_channel_param_by_app_and_platform/{appId}")
    public RestResponse<List<PayChannelParamDTO>> findPayChannelParamByAppAndPlatform(
            @PathVariable("appId") String appId,
            @RequestParam("platformChannelCode") String platformChannelCode) throws BusinessException {
        // 调用业务层方法查询数据
        List<PayChannelParamDTO> paramDTOS =
                payChannelService.queryPayChannelParamByAppAndPlatform(appId, platformChannelCode);

        return RestResponse.success(paramDTOS);
    }

    /**
     * 根据应用的appId和平台服务类型编码以及原始支付渠道编码查询原始支付渠道参数配置数据
     * @param appId 应用的appId
     * @param platformChannelCode 平台服务类型编码
     * @param payChannel 原始支付渠道编码
     * @return
     */
    @PostMapping("/find_pay_channel_param_by_app_and_platform_pay_channel/{appId}")
    public RestResponse<PayChannelParamDTO> findPayChannelParamByAppAndPlatform(
            @PathVariable("appId") String appId,
            @RequestParam("platformChannelCode") String platformChannelCode,
            @RequestParam("payChannel") String payChannel) throws BusinessException {
        // 调用业务层方法查询数据
        PayChannelParamDTO payChannelParamDTO =
                payChannelService.queryPayChannelParamByAppAndPlatform(
                        appId, platformChannelCode, payChannel);
        return RestResponse.success(payChannelParamDTO);
    }
}
